package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.TeacherBean;
import com.woniuxy.educationplatform.service.ITeacherService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RequestMapping("teacher")
@RestController
public class TeacherController {


    @Resource
    private ITeacherService teacherServiceImpl;

    @RequestMapping("/findAll")
    List<TeacherBean> findAll(){
        List<TeacherBean> all = teacherServiceImpl.findAll();
        return all;
    }

    @RequestMapping("/cancel")
    void cancel(Long id){
        TeacherBean t = new TeacherBean();
        t.setId(id);
        t.setRecommend(1);
        teacherServiceImpl.update(t);
    }

    @RequestMapping("/set")
    void set(Long id){
        TeacherBean t = new TeacherBean();
        t.setId(id);
        t.setRecommend(2);
        teacherServiceImpl.update(t);
    }

    @RequestMapping("/del")
    void del(Long id){
        teacherServiceImpl.del(id);
    }

    @RequestMapping("/find")
    List<TeacherBean> findByCondition(@RequestBody TeacherBean teacherBean){
        System.out.println(teacherBean);
        if(teacherBean.getProjectBean().getId() == null){
            teacherBean.setProjectBean(null);
        }
        return teacherServiceImpl.findByCondition(teacherBean);
    }

    @RequestMapping("/getProject")
    String[] getProject(){
        return teacherServiceImpl.getProject();
    }

    @RequestMapping("/add")
    void addTeacher(@RequestBody TeacherBean teacherBean){
        System.out.println(teacherBean);

        File f = new File("d:\\teacher\\lastTeacher.jpg");
        String path = System.currentTimeMillis()+"T.jpg";
        f.renameTo(new File("D:\\code\\idea\\ep\\src\\main\\resources\\static\\teacher\\"+path));
        teacherBean.setImage("../static/teacher/"+path);
        teacherBean.setVideoImage("../static/teacher/video.jpg");
        teacherServiceImpl.add(teacherBean);
    }

    @RequestMapping("/findTeacher")
    TeacherBean findById(Long id){

        return teacherServiceImpl.findById(id);
    }

    @RequestMapping("/update")
    void update(@RequestBody TeacherBean teacherBean){
        System.out.println(teacherBean);
        teacherServiceImpl.update(teacherBean);
    }

    @RequestMapping("/uploadImg")
    void upload(MultipartFile file) throws IOException {
        String path = "d:\\teacher\\";
        File newFile = new File(path,"lastTeacher.jpg");
        file.transferTo(newFile);
    }

    @RequestMapping("/getImg")
    void getImg(String path){
        System.out.println(path);
    }
}
